***** How large is the trade-off? Section 5.3 (last paragraph)

clear
use "${data}cleaned_data_y3.dta"
do "${blocks}construct_new_variables"

save "${temp}Aux1.dta", replace

drop if EEMP==0

*** Number of spaces in first tier every year

foreach x of num 2013/2016 {
count if WinterPool==0 & EntryYear_y1==`x'
scalar spaces_`x'=r(N)
}

display spaces_2016

*** Candidate cut offs

foreach z of num 2013/2016 {
		foreach y of num 550/595 {
			foreach x in "Female" "Male" {
			count if Gender=="`x'" & Percentage_y1>=`y'/10 & EntryYear_y1==`z'
			scalar `x'_`z'_over`y'=r(N)
		}
		scalar Total_`z'_over`y'= Female_`z'_over`y'+Male_`z'_over`y'
		scalar sqerror_`z'_`y'=(Total_`z'_over`y'-spaces_`z')^2
	}

}

foreach z of num 2013/2016 {
	scalar leastsqerror_`z'= . 
	scalar bestcutoff_`z'=.
	quietly	foreach y of num 550/595 {
		if 	sqerror_`z'_`y' < leastsqerror_`z' scalar bestcutoff_`z'=`y'
		if 	sqerror_`z'_`y' < leastsqerror_`z' scalar leastsqerror_`z'= sqerror_`z'_`y'
	}
}


*** Performance forgone

foreach z of num 2013/2016 {
summarize Percentage_y1 if WinterPool==0 & EntryYear_y1==`z', meanonly
scalar  Tot_performance_`z'=r(sum)
}

foreach z of num 2013/2016 {
summarize Percentage_y1 if Percentage_y1>bestcutoff_`z'/10  & EntryYear_y1==`z', meanonly
scalar  Sacr_performance_`z'=r(sum)-Tot_performance_`z'
scalar  Per_admit_sacr_performance_`z'=Sacr_performance_`z'/spaces_`z'
}

*** Number of students

foreach z of num 2013/2016 {
	count if WinterPool==0 & Percentage_y1<bestcutoff_`z'/10  & EntryYear_y1==`z'
	scalar wrongly_admitted_`z'=r(N)
	scalar wrongly_pooled_`z'=r(N)
	summarize Percentage_y1 if EntryYear_y1==`z'
	scalar sd_y1_`z'=r(sd)
	scalar  Per_wrong_sacr_performance_`z'=Sacr_performance_`z'/(wrongly_admitted_`z'*sd_y1_`z')
	scalar  Per_admit_sacr_performance_`z'=Sacr_performance_`z'/spaces_`z'
	count if WinterPool==1 & Percentage_y1>bestcutoff_`z'/10  & EntryYear_y1==`z'

foreach y in "Female" "Male" {	
	count if WinterPool==0 & EntryYear_y1==`z' & Gender=="`y'"
	scalar admitted_`y'_`z'=r(N)
	count if Percentage_y1>bestcutoff_`z'/10 & EntryYear_y1==`z' & Gender=="`y'"
	scalar extra_`y'_`z'=(admitted_`y'_`z'-r(N))
	scalar extra_`y'_share_`z'=((admitted_`y'_`z'-r(N))/admitted_`y'_`z')*100
}
}

*** Averages across years

foreach x in extra_Female extra_Female_share Per_admit_sacr_performance Per_wrong_sacr_performance wrongly_admitted {
scalar `x'_ave=(`x'_2013+`x'_2014+`x'_2015+`x'_2016)/4
}

tempname statistics
tempfile stats
postfile `statistics' ///
    str100 statistic extra extra_share Per_wrong_sacr_performance using "`stats'", replace

foreach x in extra_Female extra_Female_share Per_wrong_sacr_performance {
	local `x'=`x'_ave
	}

post `statistics'  ("Gender, MI subjects") (.) (.) (.)  

post `statistics' ("\quad (h = female, g = male)")  (`extra_Female') (`extra_Female_share') (`Per_wrong_sacr_performance')

postclose `statistics'

use `stats', clear

foreach x in extra extra_share Per_wrong_sacr_performance {
format `x'  %9.1fc
	}	
	
listtab * using "${tables}performance_h_group_trade_off_test.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{@{\extracolsep{2pt}} l c c c D{.}{.}{-3} D{.}{.}{-3}}" ///
    "\toprule" ///
	    " & \multicolumn{1}{c}{Additional \textit{h}} & \multicolumn{1}{c}{Additional \textit{h}, \%} & \multicolumn{1}{c}{Performance forgone} \\" ///
	"\midrule" "& (1) & (2) & (3) \\ \midrule") ///
    foot("\bottomrule" "\end{tabular}")
	
